package com.newdjk.newdoctor.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import androidx.appcompat.widget.AppCompatButton;

import com.newdjk.newdoctor.BasActivity;
import com.newdjk.newdoctor.R;
import com.newdjk.newdoctor.net.BaseComPleteObserver;
import com.newdjk.newdoctor.net.NetServices;
import com.newdjk.newdoctor.utils.Contants;
import com.newdjk.newdoctor.utils.LogUtils;
import com.newdjk.newdoctor.utils.SpUtils;
import com.newdjk.newdoctor.utils.StrUtil;
import com.newdjk.newdoctor.utils.ToastUtil;
import com.newdjk.okhttp.entity.BaseEntity;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 重置密码
 */
public class ResettingActivity extends BasActivity {

    @BindView(R.id.input_Password)
    EditText inputPassword;
    @BindView(R.id.input_new_Password)
    EditText inputNewPassword;
    @BindView(R.id.btn_submit)
    AppCompatButton btnSubmit;
    private int userId = 0;//1 = 用户id

    public static Intent getIntent(Context context, String userId) {
        Intent intent = new Intent(context, ResettingActivity.class);
        intent.putExtra("userId", userId);
        return intent;
    }

    @Override
    protected String setTitle() {
        return "找回密码";
    }

    @Override
    protected int initViewResId() {
        return R.layout.activity_resetting;
    }


    @Override
    protected void initListener() {
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submitPassword();
            }
        });
    }

    @Override
    protected void initData() {
        userId = getIntent().getIntExtra("userId", 0);
    }

    @Override
    protected void initView() {

    }

    @Override
    public boolean IsShowBackIcon() {
        return true;
    }

    /**
     * 完成
     */

    public void submitPassword() {
        if (isNull()) {

            Map<String, String> map = new HashMap<>();
            String NewPass = StrUtil.getString(inputPassword);
            String TwoPass = StrUtil.getString(inputNewPassword);
            Log.e("zdp", "isequal=" + NewPass.equals(TwoPass) + ",TwoPass=" + TwoPass + ",userId=" + userId);

            NetServices.Factory.getService().ChangeDoctorPassword(userId + "", NewPass, TwoPass
            ).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseComPleteObserver(ResettingActivity.this) {
                        @Override
                        protected void onSuccees(BaseEntity t) throws Exception {

                            SpUtils.put(Contants.Password, inputPassword.getText().toString());
                            SpUtils.put(Contants.isPasswordlogin, true);
                            Intent intent = new Intent(ResettingActivity.this, LoginActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                        }
                    });


        }
    }

    public boolean isNull() {

        if (TextUtils.isEmpty(inputPassword.getText().toString())){
            ToastUtil.setToast("请输入新密码");
            return false;
        }
        if (TextUtils.isEmpty(inputNewPassword.getText().toString())){
            ToastUtil.setToast("请输入确认密码");
            return false;
        }
        if (StrUtil.getString(inputPassword).length() < 6) {
            ToastUtil.setToast("新密码为6-12位数字或字母");
            return false;
        } else if (!StrUtil.getString(inputPassword).equals(StrUtil.getString(inputNewPassword))) {
            Log.i("zdp", "cccccc");
            ToastUtil.setToast("两次密码不一致");
            return false;
        } else if (!isValidPassword(inputPassword.getText().toString())) {
            ToastUtil.setToast("密码为6-12位数字或字母");
            return false;
        }
        return true;
    }

    private boolean isValidPassword(String password) {
        return isMatcherFinded("^[a-zA-Z0-9]{6,12}$", password);
    }

    public static boolean isMatcherFinded(String patternStr, CharSequence input) {
        Pattern pattern = Pattern.compile(patternStr);
        Matcher matcher = pattern.matcher(input);
        return matcher.find();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
